package com.sunshine.asm.oneday;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class HelloWord {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//生成一个类只需要ClassWriter组件即可
        ClassWriter cw = new ClassWriter(0);
        //通过visit方法确定类的头部信息
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC,
                "com/asm3/Comparable", null, "java/lang/Object", new String[]{"com/asm3/Mesurable"});
        //定义类的属性
        cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
                "LESS", "I", null, new Integer(-1)).visitEnd();
        cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
                "EQUAL", "I", null, new Integer(0)).visitEnd();
        cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
                "GREATER", "I", null, new Integer(1)).visitEnd();
        //定义类的方法
        cw.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "sayHello",
                "(Ljava/lang/String)V", null, null).visitEnd();
        cw.visitEnd(); //使cw类已经完成
        //将cw转换成字	数组写到文件里面去
        byte[] data = cw.toByteArray();
       
        
        ClassReader cr = new ClassReader(data);
        //cr.accept(new Print(), 0);
        cr.accept(new Print(),0);

        
        ClassReader cr1 = new ClassReader("java.util.ArrayList");
        //cr.accept(new Print(), 0);
        cr1.accept(new Print(),0);
	}
	
}
class Print implements ClassVisitor
{

	public void visit(int version, int access, String name, String signature,
			String supername, String[] exceptions) {
		// TODO Auto-generated method stub
		System.out.println("class version:"+version);
		System.out.println("class name:"+name);
		System.out.println("extends:"+supername);
	}

	public AnnotationVisitor visitAnnotation(String arg0, boolean arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	public void visitAttribute(Attribute attr) {
		// TODO Auto-generated method stub
		
	}

	public void visitEnd() {
		// TODO Auto-generated method stub
		System.out.println("=====END=========");
	}

	public FieldVisitor visitField(int access, String name, String desc,
			String signature, Object value) {
		
		System.out.println("field:"+name+"  "+value);
		System.out.println("access:"+access);
		
		return null;
	}

	public void visitInnerClass(String arg0, String arg1, String arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	public MethodVisitor visitMethod(int access, String name, String desc,
			String signature, String[] exceptions) {
		// TODO Auto-generated method stub
		System.out.println("method:"+name+"  "+desc);
		
		return null;
	}

	public void visitOuterClass(String arg0, String arg1, String arg2) {
		// TODO Auto-generated method stub
		
	}

	public void visitSource(String arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
	
}
